// pages/edit_info/index.js
const app     = getApp()
const request = require('../../utils/request.js');
const config = require('../../utils/config.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        genderArr: ['男', '女'],
        genderIdx: -1,
        userInfo:null
    },
    inputNickname(e){
        this.setData({
            'userInfo.nickname':e.detail.value
        })
    },
    genderBirth(e) {
        this.setData({
            'userInfo.birthday':e.detail.value
        })
    },
    genderChange(e) {
        this.setData({
            genderIdx:e.detail.value
        })
    },
    uploadall:function(e)
    {
          console.log(e)
         var that = this;
          
         wx.chooseMedia({
           count: 1,
           mediaType: ['image'],
           sourceType: ['album', 'camera'],
           maxDuration: 30,
           camera: 'back',
           success(res) {
               console.log(res)
             let tempFilePaths = res.tempFiles;
             for (let i = 0; i < tempFilePaths.length; i++) {
                 console.log(tempFilePaths[0]);
                     wx.uploadFile({
                        url: config.web_url + '/api/common/ossupload',
                         filePath: tempFilePaths[i]['tempFilePath'],
                         name: 'file',
                         header: {
                             "Content-Type": "multipart/form-data",
                             'token': wx.getStorageSync('token')
                         },
                         success: function (res) {
                            var data = JSON.parse(res.data);
                             console.log(data)
                           
                             if(res.statusCode==200){ 

                                 that.setData({
                                    'userInfo.avatar':data.data.fullurl
                                 })
                                
                             }else{
                                 wx.showToast({
                                   title: data.msg,
                                   icon:'none'
                                 })
                             }
                            
                            
                         },
                         fail:function(res){
                             console.log(res)
                         }
                     })
                 }
           },fail:function(res){
               console.log(res)
           }
         })    
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        app.init_userInfo(false).then((res) => {
            if(res){
              wx.setStorageSync('userInfo', res)
              this.setData({
              userInfo: res,
            })
            if(this.data.userInfo.gender==1||this.data.userInfo.gender==2){
                console.log(this.data.userInfo)
              this.setData({
                  genderIdx:this.data.userInfo.gender==1?0:1
              })
            }
            }
          });
       
    },
  
      submit(){
        var that = this;
        var json={};
        json.gender=parseInt(that.data.genderIdx)+1;
        json.avatar=that.data.userInfo.avatar;
        json.nickname=that.data.userInfo.nickname;
        json.birthday=that.data.userInfo.birthday;
        request.requestLoading(
            "/api/user/updateInfo",
            json,
            'POST',
          ).then((res) => {
             if(res.code){
              wx.navigateBack({
                  delta:1
              })
             }else{
               wx.showToast({
                 title: res.msg,
                 icon:'none'
               })
               return;
             }
           
           
      
          }).catch((err) => {
            console.error(err);
          });
      },
     updateUserInfo(){
        app.init_userInfo(true).then((res) => {
            if(res){
              wx.setStorageSync('userInfo', res)
              this.setData({
              userInfo: res,
            })
            if(res.gender==1||res.gender==2){
              this.setData({
                  genderIdx:res.gender==1?0:1
              })
            }
            }
          });
     },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    
})